package com.chain.BrotherExchange.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.LegalTenderDealList;
import com.chain.BrotherExchange.data.model.LegalTenderUserInfo;
import com.chain.BrotherExchange.utils.Androids;

import java.util.List;

/**
 * 法币交易
 */
public class LegalTenderDealViewModel extends AndroidViewModel {

    private int mCurrentPage = 1;
    private boolean mIsInitialLoad = true;

    private MutableLiveData<Resource<LegalTenderUserInfo.DataBean>> mLegalTenderUserInfo;

    private MutableLiveData<Resource<List<LegalTenderDealList.DataBean.ListBean>>> mNewLegalTenderDealList;
    private MutableLiveData<Resource<List<LegalTenderDealList.DataBean.ListBean>>> mMoreLegalTenderDealList;

    private MutableLiveData<Resource<String>> mOrderActionLiveData;

    public LegalTenderDealViewModel(@NonNull Application application) {
        super(application);
        mLegalTenderUserInfo = new MutableLiveData<>();
        mNewLegalTenderDealList = new MutableLiveData<>();
        mMoreLegalTenderDealList = new MutableLiveData<>();
        mOrderActionLiveData = new MutableLiveData<>();
    }

    public MutableLiveData<Resource<List<LegalTenderDealList.DataBean.ListBean>>>
    moreLegalTenderDealList(int type, int tokenId) {
        legalTenderDealList(type, tokenId, ++mCurrentPage, mMoreLegalTenderDealList);
        return mMoreLegalTenderDealList;
    }

    public MutableLiveData<Resource<List<LegalTenderDealList.DataBean.ListBean>>>
    newLegalTenderDealList(int type, int tokenId) {
        mCurrentPage = 1;
        legalTenderDealList(type, tokenId, mCurrentPage, mNewLegalTenderDealList);
        return mNewLegalTenderDealList;
    }

    public MutableLiveData<Resource<List<LegalTenderDealList.DataBean.ListBean>>>
    moreLegalTenderUserDealList(int type, int uid) {
        legalTenderUserDealList(type, uid, ++mCurrentPage, mMoreLegalTenderDealList);
        return mMoreLegalTenderDealList;
    }

    public MutableLiveData<Resource<List<LegalTenderDealList.DataBean.ListBean>>>
    newLegalTenderUserDealList(int type, int uid) {
        mCurrentPage = 1;
        legalTenderUserDealList(type, uid, mCurrentPage, mNewLegalTenderDealList);
        return mNewLegalTenderDealList;
    }

    /**
     * 上下架或删除我的交易单
     */
    public MutableLiveData<Resource<String>> editOrderState(int state, int id) {
        mOrderActionLiveData.setValue(Resource.loading());
        int uid = Androids.LocalConfig.getUid();
        String token = Androids.LocalConfig.getToken();
        DataRepository.get().editMyPublishTransactionOrderStatus(uid, token, id, state, result -> {
            if (result.isSucceed()) {
                mOrderActionLiveData.setValue(Resource.success(result.getMsg()));
            } else {
                mOrderActionLiveData.setValue(Resource.error(result.getMsg()));
            }
        });

        return mOrderActionLiveData;
    }

    /**
     * 用户法币个人信息
     */
    private void legalTenderUserDealList(
            int type, int uid, int page, MutableLiveData<Resource<List<LegalTenderDealList.DataBean.ListBean>>> liveData) {
        if (mIsInitialLoad) {
            liveData.setValue(Resource.loading());
        }

        DataRepository.get().legalTenderUserDealList(uid, type, page, result -> {
            LegalTenderDealList.DataBean resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                List<LegalTenderDealList.DataBean.ListBean> list = resultData.getList();
                if (list != null && !list.isEmpty()) {
                    mIsInitialLoad = false;
                    liveData.setValue(Resource.success(list));
                } else {
                    liveData.setValue(Resource.empty());
                }

            } else if (result.isNotNetwork()) {
                liveData.setValue(Resource.notNetwork());
            } else if (result.isLogonFailure()) {
                liveData.setValue(Resource.tokenError(result.getMsg()));
            } else {
                liveData.setValue(Resource.error(result.getMsg()));
            }
        });
    }

    /**
     * 法币交易列表
     */
    private void
    legalTenderDealList(
            int type, int tokenId, int page,
            MutableLiveData<Resource<List<LegalTenderDealList.DataBean.ListBean>>> liveData) {

        if (mIsInitialLoad) {
            liveData.setValue(Resource.loading());
        }

        DataRepository.get().legalTenderDealList(type, tokenId, page, result -> {
            LegalTenderDealList.DataBean resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                List<LegalTenderDealList.DataBean.ListBean> list = resultData.getList();
                if (list != null && !list.isEmpty()) {
                    mIsInitialLoad = false;
                    liveData.setValue(Resource.success(list));
                } else {
                    liveData.setValue(Resource.empty());
                }

            } else if (result.isNotNetwork()) {
                liveData.setValue(Resource.notNetwork());
            } else if (result.isLogonFailure()) {
                liveData.setValue(Resource.tokenError(result.getMsg()));
            } else {
                liveData.setValue(Resource.error(result.getMsg()));
            }
        });
    }

    /**
     * 用户法币信息
     */
    public MutableLiveData<Resource<LegalTenderUserInfo.DataBean>> legalTenderUserInfo(int uid) {
        mLegalTenderUserInfo.setValue(Resource.loading());
        DataRepository.get().legalTenderUserInfo(uid, result -> {
            if (result.isSucceed()) {
                mLegalTenderUserInfo.setValue(Resource.success(result.getData()));
            } else {
                mLegalTenderUserInfo.setValue(Resource.error(result.getMsg()));
            }
        });
        return mLegalTenderUserInfo;
    }
}
